package hd.util.http.entity;

import android.support.annotation.NonNull;

import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;

import java.io.File;
import java.io.UnsupportedEncodingException;

/**
 * Entity
 * <p/>
 * Created by Haidy on 15/4/13.
 */
public class HttpEntityBody extends MultipartEntity {

    /**
     * 添加文本表单
     *
     * @param name  name
     * @param value value
     */
    public void addForm(@NonNull String name, @NonNull String value) {
        try {
            addPart(name, new StringBody(value));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 添加文件
     *
     * @param name name
     * @param file file
     */
    public void addForm(@NonNull String name, @NonNull File file) {
        if (file.exists()) {
            addPart(name, new FileBody(file));
        } else {
            throw new RuntimeException("file is not exist");
        }
    }

}
